<?php

//------------------------
// 商品属性控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\GoodsAttr as GoodsAttrModel;
use app\common\model\Goods as GoodsModel;
use think\Config;
use think\Db;

class GoodsAttr extends AdminBase
{
    protected $GoodsAttrModel;
    protected $GoodsModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsAttrModel               = new GoodsAttrModel();
        $this->GoodsModel                   = new GoodsModel();
    }



    // 编辑操作
    public function editProc(){
        $data  = $this->request->param('');

        // 如果商品类型改变了，就删除原来商品的所有属性
        $old_type_id = $this->GoodsModel->field('type_id')->where('id',$data['id'])->find();

        if ($data['type_id'] != $old_type_id->type_id) {
            $this->GoodsAttrModel->where('goods_id',$data['id'])->delete();
        }

        /*******************************类型*******************************/
        //根据传入的scene生成不同的场景
        $validate_result = $this->validate($data, 'Goods.' . $data['scene']);

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // allowField表示过滤非数据表中字段的数据
        $this->GoodsModel->allowField(true)->save($data,$data['id']);


        /*******************************属性*******************************/

        // 处理新属性
        $newData = array();
        $i       = 0;

        $attr_value = isset($data['attr_value'])? $data['attr_value'] : '';
        $attr_price = isset($data['attr_price'])? $data['attr_price'] : '';

        if($attr_value){
            foreach ($attr_value as $k => $v){
                foreach ($v as $k1 => $v1){

                    if(empty($v1)){
                        continue ;
                    }

                    $newData[$i] = array(
                        'goods_id' => $data['id'],
                        'attr_id'  => $k,
                        'value'    => str_replace("\n","",$v1),
                        'price'    => !empty($attr_price[$k][$k1])?$attr_price[$k][$k1]:"0",
                    );

                    $validate_result = $this->validate($newData[$i], 'GoodsAttr');

                    if ($validate_result !== true) {
                            $this->error($validate_result);
                    }

                    $i = ++$i;
                }
            }
        }

        $addRes = $this->GoodsAttrModel->saveAll($newData);


        // 更新旧属性
        $oldData = array();
        $j       = 0;

        $old_attr_value = isset($data['old_attr_value'])? $data['old_attr_value'] : '';
        $old_attr_price = isset($data['old_attr_price'])? $data['old_attr_price'] : '';

        if($old_attr_value){
            foreach ($old_attr_value as $k => $v){
                foreach ($v as $k1 => $v1){

                    // 旧属性中如果没有值的，就整条删掉
                    if(empty($v1)){
                        $this->GoodsAttrModel->where('id',$k1)->delete();
                    }

                    $oldData[$j] = array(
                        'id'       => $k1,
                        'goods_id' => $data['id'],
                        'attr_id'  => $k,
                        'value'    => str_replace("\n","",$v1),
                        'price'    => !empty($old_attr_price[$k][$k1])?$old_attr_price[$k][$k1]:"0",
                    );

                    $validate_result = $this->validate($oldData[$j], 'GoodsAttr');

                    if ($validate_result !== true) {
                            $this->error($validate_result);
                    }

                    $j = ++$j;
                }
            }
        }

        $editRes = $this->GoodsAttrModel->saveAll($oldData);


        if (  $editRes === false || $addRes === false ) {
            $this->error($this->GoodsAttrModel->getError());
        } else {
            $this->success('保存成功');
        }
    }









}